

#include <stdio.h>
#include <stdlib.h>
#include "structures.h"



//malloc de um novo elemento do tipo noLista
//percorrer a lista até ao úlitmo
//colocar o p_next do ultimo para o novo elemento
//o novo elemento passa a ser o último

listNode * add_user_old(USER * user, listNode * list) {
    listNode * newNode = (listNode*) malloc(sizeof (listNode));
    newNode->user = user;
    newNode->next = NULL;

    if (list == NULL)
        return newNode;

    // percorrer ate ao ultimo
    listNode * tmp = list;
    while (tmp->next) {
        tmp = tmp->next;
    }
    tmp->next = newNode;
    return list;
}

/*
 * Código tirado dos slides (CHTP4_12_01)
 */
listNode * add_user_sort_mec(USER * user, listNode * list) {
    listNode * newNode = (listNode*) malloc(sizeof (listNode));
    newNode->user = user;
    newNode->next = NULL;

    listNode * currentPtr = list;
    listNode * prevPtr = NULL;
    while (currentPtr != NULL && newNode->user->mec_number > currentPtr->user->mec_number) {
        prevPtr = currentPtr;
        currentPtr = currentPtr->next;
    }

    if (prevPtr == NULL) {
        newNode->next = list;
        list = newNode;
    } else {
        prevPtr->next = newNode;
        newNode->next = currentPtr;
    }

    return list;
}

/*
 * Inserção ordenada por nome:
 * 
 * PARA RELATÓRIO: A comparação dos nomes é deixada a cargo da função strcmp(...)
 * 
 */
listNode * add_user_sort_name(USER * user, listNode * list) {
    listNode * newNode = (listNode*) malloc(sizeof (listNode));
    newNode->user = user;
    newNode->next = NULL;

    listNode * currentPtr = list;
    listNode * prevPtr = NULL;
    while (currentPtr != NULL && strcmp(newNode->user->name, currentPtr->user->name) > 0) {
        prevPtr = currentPtr;
        currentPtr = currentPtr->next;
    }

    if (prevPtr == NULL) {
        newNode->next = list;
        list = newNode;
    } else {
        prevPtr->next = newNode;
        newNode->next = currentPtr;
    }

    return list;
}

void delete_user(int mec, listNode * list) {
    if (list == NULL) {
        printf("A lista esta vazia: impossivel remover\n");
    }
    listNode * prevPtr;
    listNode * currPtr;
    listNode * tmpPtr;

    if (mec == list->user->mec_number) {
        tmpPtr = list; // tmp e o que queremos remover
        list = list->next;
        free(tmpPtr);
    } else {
        prevPtr = list;
        currPtr = list->next;
        while (currPtr != NULL && mec != currPtr->user->mec_number) {
            prevPtr = currPtr;
            currPtr = currPtr->next;
        }
        if (currPtr != NULL) {
            tmpPtr = currPtr;
            prevPtr->next = currPtr->next;
            free(tmpPtr);
            printf("Utilizador eliminado com sucesso.\n");
        } else {
            printf("Utilizador inexiste!\n");
        }
    }
}

/*
 * verificar se utilizador existe
 */

USER * check_user_mec(int mec_number, listNode * list) {
    // percorrer a lista ate encontrar um username existente
    while (list) {
        if (mec_number == list->user->mec_number)
            return list->user;
        list = list->next;
    }
    // se nao existir retorna falso
    return NULL;
}

USER * check_user(char * username, listNode * list) {
    // percorrer a lista ate encontrar um username existente
    while (list) {
        if (strcmp(username, list->user->username) == 0)
            return list->user;
        list = list->next;
    }
    // se nao existir retorna falso
    return NULL;
}

listNode * convertMec2Name(listNode * MECS) {
    listNode * NAMES = NULL;
    listNode * aux_mecs = MECS;
    while (aux_mecs != NULL) {
        USER * user = aux_mecs->user;
        NAMES = add_user_sort_name(user, NAMES);
        aux_mecs = aux_mecs->next;
    }
    // free memory
    free_list(MECS);
    return NAMES;
}

listNode * convertName2Mec(listNode * NAMES) {
    listNode * MECS = NULL;
    listNode * aux_names = NAMES;
    while (aux_names != NULL) {
        USER * user = aux_names->user;
        MECS = add_user_sort_mec(user, MECS);
        aux_names = aux_names->next;
    }
    // free memory
    free_list(NAMES);
    return MECS;
}

void free_list(listNode * list) {
    while (list) {
        listNode * del = list;
        list = list->next;
        free(del);
    }
}

int get_list_size(listNode * list) {
    int size = 0;
    listNode * aux = list;
    while (aux != NULL) {
        aux = aux->next;
        size++;
    }
    return size;
}
